#include "itembutton.h"
#include <QHBoxLayout>
#include <QDebug>
#include "../main/mainwindow.h"
#include "../../models/usermodel.h"

//用途1：好友列表里面的好友，包含头像、昵称、部分状态的item，每个item表示一个好友
//用途2：chat左侧的列表
ItemButton::ItemButton(QWidget *parent):QLabel(parent)
{
    this->styleSelf();
}

void ItemButton::styleSelf(){
    //@todo last update设置左边的边距，调整样式
    //this->setStyleSheet("margin-left:5px");

}

void ItemButton::draw(){
    QLabel* spaceLabel = new QLabel();//空白标签，站位
    avatarLabel = new QLabel();//头像
    profileNicknameLabel = new QLabel();//昵称
    this->unreadLabel = new QLabel();//未读数
    profileStatusLabel = new QLabel();//状态

    QLabel* nicknameAndUnreadLabel = new QLabel();//昵称+未读
    nicknameAndStatusLabel = new QLabel();//昵称+未读+状态

    spaceLabel->setFixedSize(10, 40);
    //昵称样式
    profileNicknameLabel->setText(this->userProfile.username);
    profileNicknameLabel->setFixedSize(140, 20);
//    profileNicknameLabel->setStyleSheet("background-color:rgb(0, 255, 255)");//blue
    //未读样式
    this->unreadLabel->setFixedSize(17, 17);
    this->unreadLabel->setAlignment(Qt::AlignHCenter);
    this->unreadLabel->setStyleSheet("QLabel{background:transparent;border-image:url(:/icon/source/main/unread.png);color:white;font-size:12px;};");

    //状态样式
    profileStatusLabel->setText(this->statusText);
    profileStatusLabel->setStyleSheet("color:#8d8d8d");
    //昵称+未读
    nicknameAndUnreadLabel->setFixedSize(130, 20);
    //头像label的样式
    avatarLabel->setFixedSize(40, 40);//长40，高40
    QString avatar = this->userProfile.avatar;
    if(avatar == ""){
        avatar = "/icon/source/profile/default.png";
    }
    avatarLabel->setStyleSheet("border-image:url(:" + avatar + ")");
    //昵称+未读+状态
    nicknameAndStatusLabel->setFixedSize(150, 40);
//    nicknameAndStatusLabel->setStyleSheet("background-color:rgb(0, 255, 0)");//green

    //1、当前对象放置一个水平布局(就是左边一个，右边一个挨着走，大家的起点都在同一水平线上)
    QHBoxLayout *itemLayout = new QHBoxLayout();
    this->setLayout(itemLayout);

    //2、布局里面左边放一个头像的label, 右边放一个昵称+未读+状态的label
    itemLayout->addWidget(spaceLabel);
    itemLayout->addWidget(avatarLabel);
    itemLayout->addWidget(nicknameAndStatusLabel);
    itemLayout->setMargin(0);
    itemLayout->setSpacing(0);

    //4、昵称+未读+状态的label的样式-里面放一个垂直布局
    QVBoxLayout *nicknameAndStatusLayout = new QVBoxLayout();
    nicknameAndStatusLabel->setLayout(nicknameAndStatusLayout);

    //5、昵称+未读和状态放在垂直布局里面
    nicknameAndStatusLayout->addWidget(nicknameAndUnreadLabel);
    if(this->statusText != ""){
        nicknameAndStatusLayout->addWidget(profileStatusLabel);
    }
    nicknameAndStatusLayout->setMargin(0);
    nicknameAndStatusLayout->setSpacing(0);

    //6、昵称+未读放在一个水平布局里面
    QHBoxLayout* nicknameAndUnreadLayout = new QHBoxLayout();
    nicknameAndUnreadLabel->setLayout(nicknameAndUnreadLayout);
    nicknameAndUnreadLayout->addWidget(profileNicknameLabel);
    nicknameAndUnreadLayout->addWidget(unreadLabel);
    nicknameAndUnreadLayout->setMargin(0);
    nicknameAndUnreadLayout->setSpacing(0);
}

void ItemButton::setUid(int u){
    this->uid = u;
}

void ItemButton::setAvatar(QString avatar){
    this->avatar = avatar;
}

void ItemButton::setNickanme(QString nickname){
    this->nickname = nickname;
}

void ItemButton::setStatusText(QString statusT){
    this->statusText = statusT;
}

void ItemButton::setRightInfo(UserInfo *info){
    this->rightInfo = info;
}

void ItemButton::setUserProfile(responseUserInfo profile){
    this->userProfile = profile;
}

void ItemButton::setUseType(int type){
    this->useType = type;
}

int ItemButton::getUseType(){
    return this->useType;
}


void ItemButton::mousePressEvent(QMouseEvent *event){
    MainWindow *mainPtr = (MainWindow*)parent()->parent()->parent()->parent()->parent()->parent()->parent();
    //联系人页面的单击事件
    if(this->getUseType() == ItemButton::USE_TYPE_FOR_CONTACTS){
        mainPtr->contacts->clickItemButtonEventForContactsPage(this);
    }else if (this->getUseType() == ItemButton::USE_TYPE_FOR_CHAT) {
        mainPtr->chat->clickItemButtonEventForChatPage(this);
    }
}

//contacts双击事件，从contacts跳转到chat
void ItemButton::mouseDoubleClickEvent(QMouseEvent *){
    //联系人页面的双击事件
    if(this->getUseType() == ItemButton::USE_TYPE_FOR_CONTACTS){
        ItemButton* item;
        MainWindow *mainPtr = (MainWindow*)parent()->parent()->parent()->parent()->parent()->parent()->parent();
        mainPtr->on_chatButton_clicked();
        item = mainPtr->chat->addLeftScrollItem(this->userProfile, 0);
        mainPtr->chat->clickItemButtonEventForChatPage(item);
    }else if (this->getUseType() == ItemButton::USE_TYPE_FOR_CHAT) {

    }
}


/**
 * @brief ItemButton::refreshChatUnreadNum 刷新红点
 * @param str
 */
void ItemButton::refreshChatUnreadNum(QString str){
    this->unreadLabel->setText(str);
    if(str == "" || str == "0"){
        this->unreadLabel->setVisible(false);
    }else{
        this->unreadLabel->setVisible(true);
    }
}


ItemButton::~ItemButton(){
    delete avatarLabel;
    delete nicknameAndStatusLabel;
}
